<?php 	
    $absolute_path = __FILE__;
	$path_to_file = explode( 'wp-content', $absolute_path );
	$path_to_wp = $path_to_file[0];
	require_once( $path_to_wp.'/wp-load.php' );
	require_once('includes/recaptchalib.php');	

	$private_key = get_option('recaptcha_private_key');
	
	$re_captcha_empty = 'Please fill in the security code.';	
	$re_captcha_error = 'The value you entered in the security field didn\'t match. Please try again.';
	$mail_success = 'The e-mail was sent successfully.';
	$mail_error = 'The e-mail could not be sent. Please try again.';
	

	if ( isset($_POST["recaptcha_response_field"]) )
	{
		if ( $_POST["recaptcha_response_field"] != '' )
		{
			$re_captcha_match = recaptcha_check_answer($private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
			if ($re_captcha_match->is_valid)
			{
				if ( isset($_POST['destination_email'])) $to = $_POST['destination_email'];
				if ( isset($_POST['name'])) $name = $_POST['name'];
				if ( isset($_POST['email'])) $email = $_POST['email'];
				if ( isset($_POST['company'])) $company = $_POST['company'];
				if ( isset($_POST['phone'])) $phone = $_POST['phone'];
				if ( isset($_POST['message'])) $message = $_POST['message'];
				
				if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) 
				{
					$headers  = "MIME-Version: 1.0\r\n";
					$headers .= "From: ".$email." \r\n";
					$headers .= "Reply-to:".$email."\r\n";
					$headers .= "X-Priority: 3\r\n";
					$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
					$headers .= "X-Mailer: PHP mailer\r\n";

					if ( isset($name) )
						$mail  = 'Name: '.$name.'<br />';
						$mail .= 'Subject: A new message from your website.<br />';
						
					if ( isset($message) )
					{
						$mail .= 'Message: '.$message;
						
						if ( $company != '' ) 	$mail .= '<br />Company: '.$company;
						if ( $phone != '' ) 	$mail .= '<br />Phone: '.$phone;
					}
						
					$subject = 'A new message from you\'re website.';
					
					if ( $mail ) $mail_sender = mail($to, $subject, $mail, $headers);
			
					if ( $mail_sender == 1 )
					{
						echo $mail_success;						
					}
					else
					{
						echo $mail_error;							
					}

				}
				
			} 
			else
			{
				echo $re_captcha_error;
			}
		} // END if ( $_POST["recaptcha_response_field"] != '' )
		else 
		{
			echo $re_captcha_empty;
		}
	} // END if ( isset($_POST["recaptcha_response_field"]) )
	
	else
	{
		if ( isset($_POST['destination_email'])) $to = $_POST['destination_email'];
		if ( isset($_POST['name'])) $name = $_POST['name'];
		if ( isset($_POST['email'])) $email = $_POST['email'];
		if ( isset($_POST['company'])) $company = $_POST['company'];
		if ( isset($_POST['phone'])) $phone = $_POST['phone'];
		if ( isset($_POST['message'])) $message = $_POST['message'];
		
		if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) 
		{
			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "From: ".$email." \r\n";
			$headers .= "Reply-to:".$email."\r\n";
			$headers .= "X-Priority: 3\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
			$headers .= "X-Mailer: PHP mailer\r\n";

			if ( isset($name) )
				$mail  = 'Name: '.$name.'<br />';
				$mail .= 'Subject: A new message from your website.<br />';
				
			if ( isset($message) )
			{
				$mail .= 'Message: '.$message;
				
				if ( $company != '' ) 	$mail .= '<br />Company: '.$company;
				if ( $phone != '' ) 	$mail .= '<br />Phone: '.$phone;
			}
			
			$subject = 'A new message from you\'re website.';
			
			if ( $mail ) $mail_sender = mail($to, $subject, $mail, $headers);
	
			if ( $mail_sender == 1 )
			{
				echo $mail_success;						
			}
			else
			{
				echo $mail_error;							
			}

		}
	} // END else
	
?>